//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pss;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.PssProductCodeDao;
import com.aidong.model.wills.PssProductCode;
import com.aidong.util.ExportXls;
import com.aidong.web.controller.BaseController;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"product"})
public class ProductController extends BaseController {
    @Resource
    PssProductCodeDao pssProductCodeDao;

    public ProductController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("pss/product/list");
        mv.addObject("productTypeLst", this.getTypeList("productType"));
        mv.addObject("saleFlgLst", this.getTypeList("saleFlgLst"));
        return mv;
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "productCode",required = false) String productCode, @RequestParam(value = "productName",required = false) String productName, @RequestParam(value = "spell",required = false) String spell, @RequestParam(value = "productType",required = false) String productType, @RequestParam(value = "saleFlg",required = false) String saleFlg, @RequestParam(value = "status",required = false) String status, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.eq("productCode", productCode);
        cd.like("productName", "%" + productName + "%");
        cd.like("spell", "%" + spell + "%");
        cd.eq("productType", productType);
        cd.eq("saleFlg", saleFlg);
        cd.eq("status", status);
        this.pssProductCodeDao.queryPageList(pg, cd, (new OrderSet()).asc("productCode"));
        ModelAndView mv = new ModelAndView("pss/product/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        PssProductCode product = new PssProductCode();
        ModelAndView mv = new ModelAndView("pss/product/edit");
        mv.addObject("pssProductCode", product);
        mv.addObject("productUnit", this.getTypeList("productUnit"));
        mv.addObject("productTypeLst", this.getTypeList("productType"));
        mv.addObject("purcContract", this.getTypeList("purcContract"));
        mv.addObject("saleFlgLst", this.getTypeList("saleFlgLst"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("ppsProductCode") PssProductCode product, @RequestParam(value = "productCode",required = false) String productCode, @RequestParam(value = "productName",required = false) String productName, @RequestParam(value = "productType",required = false) String productType, @RequestParam(value = "unit",required = false) String unit, @RequestParam(value = "spell",required = false) String spell, @RequestParam(value = "price",required = false) String price, @RequestParam(value = "purcPrice",required = false) String purcPrice, @RequestParam(value = "defaultContract",required = false) String defaultContract, @RequestParam(value = "note",required = false) String note) {
        if(this.chkNull(product.getProductCode())) {
            return this.getResult(false, "请输入商品编号");
        } else if(this.chkNull(product.getProductName())) {
            return this.getResult(false, "请输入商品名称");
        } else if(this.pssProductCodeDao.exists(product.getProductCode())) {
            return this.getResult(false, "商品编号已存在");
        } else {
            product.setStatus(Integer.valueOf(1));
            product.setCreateUser(this.getLoginUserCode());
            product.setCreateTime(new Date());
            product.setUpdateTime(new Date());

            try {
                this.pssProductCodeDao.add(product);
                this.addUserLog("商品信息设置", "编号:" + product.getProductCode() + ",名称:" + product.getProductName());
            } catch (Exception var12) {
                this.log.error("add PpsProductCode", var12);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "保存完成");
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("productCode") String productCode) {
        PssProductCode product = (PssProductCode)this.pssProductCodeDao.getById(productCode);
        if(product == null) {
            return this.showAlert("没有这个记录");
        } else {
            ModelAndView mv = new ModelAndView("pss/product/edit");
            mv.addObject("pssProductCode", product);
            mv.addObject("productUnit", this.getTypeList("productUnit"));
            mv.addObject("productTypeLst", this.getTypeList("productType"));
            mv.addObject("purcContract", this.getTypeList("purcContract"));
            mv.addObject("saleFlgLst", this.getTypeList("saleFlgLst"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("product") PssProductCode product) {
        if(this.chkNull(product.getProductCode())) {
            return this.getResult(false, "请输入商品编号");
        } else if(this.chkNull(product.getProductName())) {
            return this.getResult(false, "请输入商品名称");
        } else {
            PssProductCode product1 = (PssProductCode)this.pssProductCodeDao.getById(product.getProductCode());
            if(product1 == null) {
                return this.getResult(false, "没有这个记录");
            } else {
                product1.setProductName(product.getProductName());
                product1.setProductType(product.getProductType());
                product1.setUnit(product.getUnit());
                product1.setSpell(product.getSpell());
                product1.setPrice(product.getPrice());
                product1.setAllowanceTypePrice1(product.getAllowanceTypePrice1());
                product1.setAllowanceTypePrice2(product.getAllowanceTypePrice2());
                product1.setAllowanceTypePrice3(product.getAllowanceTypePrice3());
                product1.setAllowanceTypePrice4(product.getAllowanceTypePrice4());
                product1.setAllowanceTypePrice5(product.getAllowanceTypePrice5());
                product1.setPurcPrice(product.getPurcPrice());
                product1.setDefaultContract(product.getDefaultContract());
                product1.setGroundingDate(product.getGroundingDate());
                product1.setUndercarriageDate(product.getUndercarriageDate());
                product1.setSaleFlg(product.getSaleFlg());
                product1.setNote(product.getNote());
                product1.setStatus(product.getStatus());
                product1.setUpdateTime(new Date());

                try {
                    this.pssProductCodeDao.update(product1);
                    this.addUserLog("修改系统设置", "编号:" + product1.getProductCode() + ",名称:" + product1.getProductName());
                } catch (Exception var4) {
                    this.log.error("edit setting", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("productCode") String productCode) {
        PssProductCode product = (PssProductCode)this.pssProductCodeDao.getById(productCode);
        if(product == null) {
            return this.getResult(false, "没有这个记录");
        } else {
            product.setStatus(Integer.valueOf(0));
            product.setUpdateTime(new Date());

            try {
                this.pssProductCodeDao.update(product);
                this.addUserLog("删除系统设置", "编号:" + product.getProductCode() + ",名称:" + product.getProductName());
            } catch (Exception var4) {
                this.log.error("del setting", var4);
                return this.getResult(false, "删除发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }

    @RequestMapping({"expt"})
    public ModelAndView expt(@RequestParam(value = "productCode",required = false) String productCode, @RequestParam(value = "productName",required = false) String productName, @RequestParam(value = "spell",required = false) String spell, @RequestParam(value = "productType",required = false) String productType, @RequestParam(value = "saleFlg",required = false) String saleFlg, @RequestParam(value = "status",required = false) String status, HttpServletResponse response) {
        try {
            Pagination e = new Pagination(1, 20000);
            e.setPage(Integer.valueOf(1), Integer.valueOf(1));
            ConditionMap cd = new ConditionMap();
            cd.eq("productCode", productCode);
            cd.like("productName", "%" + productName + "%");
            cd.like("spell", "%" + spell + "%");
            cd.eq("productType", productType);
            cd.eq("saleFlg", saleFlg);
            cd.eq("status", status);
            this.pssProductCodeDao.queryPageList(e, cd, (new OrderSet()).asc("productCode"));
            ExportXls xls = new ExportXls(response, "商品信息", "商品信息");
            xls.addHead(new String[]{"商品编码", "商品名称", "商品类别", "计量单位", "标准售价", "员工价", "会员价", "采购价", "采购合同号", "上架日期", "下架日期", "卖品/非卖品", "备注"});
            xls.setWidth(new int[]{10, 25, 10, 15, 10, 10, 10, 10, 25, 10, 10, 10, 20});
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Iterator i$ = e.getResultList().iterator();

            while(i$.hasNext()) {
                PssProductCode pc = (PssProductCode)i$.next();
                xls.addRow(new Object[]{pc.getProductCode(), pc.getProductName(), this.cache.getValue("productType", pc.getProductType()), this.cache.getValue("productUnit", pc.getUnit()), pc.getPrice(), pc.getAllowanceTypePrice1(), pc.getAllowanceTypePrice2(), pc.getPurcPrice(), this.cache.getValue("purcContract", pc.getDefaultContract()), sdf2.format(pc.getGroundingDate()), sdf2.format(pc.getUndercarriageDate()), this.cache.getValue("saleFlgLst", pc.getSaleFlg()), pc.getNote()});
            }

            xls.close();
            this.addUserLog("导出商品信息", e.getResultList().size() + "条记录");
        } catch (Exception var14) {
            this.log.error("import", var14);
        }

        return null;
    }
}
